/*
Philip Dilse
dilsep
Assignment 3

unit test for embargo card
*/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include "dominion.h"
#include "dominion_helpers.h"

int main() {
    int i;
    int handCount;
    int startHandCount = 5;
    int maxHandCount = 300;
    int player = 0;
    int startCoins;
    int choice1;
    int handPos;
    int expectedReturn, actualReturn;
    int startEmbargoTokens;

    struct gameState *gs = newGame();
 
    // set up random number gen
    srand(time(NULL));

    gs->whoseTurn = player;
    for (handCount = startHandCount; handCount <= maxHandCount; handCount++) {
        
        // randomly set start coins
        startCoins = (rand() % 10) + 1;
        gs->coins = startCoins;

        // assign embargo tokens(3% chance of starting at 1), every 5th supply stack out of play
        for (i = 0; i <= treasure_map; i++) {
            if (i % 5 == 0) { 
                gs->supplyCount[i] = -1;
            } else { 
                gs->supplyCount[i] = 1;
            }
            if (rand() % 100 < 3) {
                gs->embargoTokens[i] = 1;
            } else {
                gs->embargoTokens[i] = 0;
            }
        }

        // init hand 
        gs->handCount[player] = handCount;
        for (i = 0; i < handCount; i++) {
            gs->hand[player][i] = gold;
        }

        // swap in embargo card
        handPos = rand() % handCount;
        gs->hand[player][i] = embargo;

        // randomly select choice1
        choice1 = rand() % handCount;
        startEmbargoTokens = gs->embargoTokens[choice1];

        if (gs->supplyCount[choice1] == -1) {
            expectedReturn = -1;
        } else {
            expectedReturn = 0;
        }

        actualReturn = cardEffect(embargo, choice1, 0, 0, gs, handPos, &i);

        if (actualReturn == -1) {
            if (expectedReturn != -1) { 
                printf("embargo() error: actual=%d expected=%d\n", actualReturn, expectedReturn);
            }
        } else {
            // check coins
            if (startCoins + 2 != gs->coins)
                printf("embargo() error: coins start=%d after=%d\n", startCoins, gs->coins);

            // check hand count one less
            if (handCount - 1 != gs->handCount[player])
                printf("embargo() error: handcount start=%d after=%d\n", handCount, gs->handCount[player]);

            // check embargo tokens
            if (startEmbargoTokens + 1 != gs->embargoTokens[choice1]) {
                printf("embargo() error: tokens start=%d after=%d\n", startEmbargoTokens, gs->embargoTokens[choice1]);
            }
        }
    }

    free(gs);
    return 0;
}
